package com.ibm.spatiotemp.test.requestServer;

import java.text.ParseException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.ibm.spatiotemp.server.framework.request.Request;
import com.ibm.spatiotemp.tools.DateParser;

public class HttpRequestParser {

	public static Logger logger = Logger.getLogger(HttpRequestParser.class);
	public static HashSet<String> numberformatkeyset = new HashSet<String>();
	public static HashSet<String> datekeyset = new HashSet<String>();
	public static HashSet<String> vectorkeyset = new HashSet<String>();
	
	static{
		numberformatkeyset.add("bbox");
		numberformatkeyset.add("position");
		numberformatkeyset.add("distance");
		
		datekeyset.add("start");
		datekeyset.add("end");
		datekeyset.add("time");
		
		vectorkeyset.add("data");
	}
	
	//bbox,position,distance need not \"\"
	public static Request paserHttpRequestParameters(Map requestparamap ){
		StringBuffer buf = new StringBuffer();
		
		@SuppressWarnings("unchecked")
		Set<String> keys = requestparamap.keySet();
		String sourceid = (String) requestparamap.get("sourceid");
		String tenantid = (String) requestparamap.get("tenantid");
		buf.append(String.format("\"%s\":\"%s\"", "sourceid", sourceid));
		for (String key : keys) {
			String value = (String) requestparamap.get(key);
			if(numberformatkeyset.contains(key)){
				buf.append(String.format(",\"%s\":%s", key, value));
			}
			else if(datekeyset.contains(key))
			{
				buf.append(String.format(",\"%s\":\"%s\"", key, DateParser.transformDateFormat(value)));
			}
			else if(vectorkeyset.contains(key))
			{
				buf.append(String.format(",\"%s\":%s", key, value));
			}
			else
			{
				buf.append(String.format(",\"%s\":\"%s\"", key, value));
			}
		}
		String bodyjson = String.format("{%s}", buf.toString());
		Request req = null;
		try {
			req = new Request(bodyjson);
		} catch (ParseException e) {
			logger.error("parse requst error, jsonstring="+bodyjson);
		}
		
		String querytype = (String) requestparamap.get("queryType");
		
		return req;
	}
	
}
